#ifndef _N2F_RENDER_IW2D_SPRITEIMAGE_H_
#define _N2F_RENDER_IW2D_SPRITEIMAGE_H_

#include "..\..\core.h"
#include "..\..\Element.h"
#include "..\..\..\iw2d\h\Iw2D.h"
#include "..\..\Screen.h"
#include <math.h>

namespace N2F
{
	namespace Iw3D
	{
		/*!
		 * \class SpriteImage
		 *
		 * \brief A base class for display spritesheets with animations.
		 *
		 * \author Andrew Male
		 * \email amale@zibings.com
		 * \date 9/2/2013
		 */
		class SpriteImage : public Element
		{
		private:
			struct AnimInfo
			{
				int startFrame, endFrame;
				int64 duration;

				AnimInfo();
				AnimInfo(int startFrame, int endFrame, int64 duration);
			};

			struct AnimTracker
			{
				int64 start;
				int index, frame;

				AnimTracker(int64 start, int index);
			};

			typedef std::map<int, AnimInfo> AnimList;
			typedef std::pair<int, AnimInfo> AnimListItem;
			typedef std::map<int, AnimInfo>::iterator AnimListIter;

		public:
			SpriteImage(const ScaleFactor &scaleFactor);
			~SpriteImage();

			void setFile(const char *file);
			const char *getFile() const;

			const float getCellWidth() const;
			void setCellWidth(float width);

			const float getCellHeight() const;
			void setCellHeight(float height);

			const float getCellHorizontalPadding() const;
			void setCellHorizontalPadding(float padding);

			const float getCellVerticalPadding() const;
			void setCellVerticalPadding(float padding);

			void setIdleFrame(int frame);
			void setAnimation(int index, int startFrame, int endFrame, int64 duration);
			void triggerAnimation(int index);
			void stopAnimations();
			void updateAnimation(int64 delta);
			bool animComplete();

			virtual void draw(int64 delta);
			virtual void draw(int64 delta, CIwFVec2 offset);
			virtual void draw(int64 delta, CIwFVec2 offset, float angle);
			virtual void draw(int64 delta, CIwFVec2 offset, float angle, float scale);

			virtual void OnKill();
			virtual void OnTouch(InputPoint point);
			virtual bool isHit(CIwFVec2 point);

		protected:
			CIwFVec2 translateSize();
			CIwFVec2 translateSize(float scale);
			CIwFVec2 translatePosition();
			CIwFVec2 translatePosition(float scale);

			int getDrawFrame();
			CIwFVec2 getFrameRegion(int drawFrame);
			CIwFVec2 getFrameSize();

			float cellWidth, cellHeight, cellVerticalPadding, cellHorizontalPadding;
			int numFrames, numCols, numRows, currentAnimation, idleFrame;			
			AnimTracker animProgress;
			AnimList animations;
			Rectangle *hitBox;
			const char *file;
			CIw2DImage *img;
		};
	}
}

#endif
